<!-- 店铺编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="shopModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="shopModalApp.shop"
        :rules="shopModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="拥有者:" name="userId">
              <m-entity-select
                :default-value="shopModalApp.shop.userName"
                v-model:value="shopModalApp.shop.userId"
                placeholder="请选择拥有者"
                module="sys"
                system="1"
                entity="user"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="店铺描述:" name="description">
              <a-input
                v-model:value="shopModalApp.shop.description"
                placeholder="请输入店铺描述"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="地址:" name="adress">
              <a-input
                v-model:value="shopModalApp.shop.adress"
                placeholder="请输入地址"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="服务态度:" name="serviceAttitude">
              <a-input
                v-model:value="shopModalApp.shop.serviceAttitude"
                placeholder="请输入服务态度"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="粉丝数量:" name="fans">
              <a-input
                v-model:value="shopModalApp.shop.fans"
                placeholder="请输入粉丝数量"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="shopModalApp.display">
                <teleport to="#shopEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="shopListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(shopModalApp.shop)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!shopModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(shopModalApp.shop)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {ShopService} from "@/views/mall/merchant/shop/shopService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';


import MEntitySelect from "@/components/MEntity/entitySelect";

export default defineComponent({
  components: {
    MEntitySelect,
  },
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let shopListApp = inject('shopListApp', reactive({}));
    let shopGrid = inject('shopGrid', reactive({}))
    const shopModalApp = reactive({
      id: null,
      shop: {},
      isEdit: false,
      display: false
    });
    shopModalApp.rules = {
      userName: [
        {
          required: false,
          type: 'string',
          message: '请输入拥有者',
          trigger: 'blur'
        }
      ],
      userId: [
        {
          required: false,
          type: 'number',
          message: '请输入拥有者',
          trigger: 'blur'
        }
      ],
      description: [
        {
          required: false,
          type: 'string',
          message: '请输入店铺描述',
          trigger: 'blur'
        }
      ],
      adress: [
        {
          required: false,
          type: 'string',
          message: '请输入地址',
          trigger: 'blur'
        }
      ],
      serviceAttitude: [
        {
          required: false,
          type: 'string',
          message: '请输入服务态度',
          trigger: 'blur'
        }
      ],
      fans: [
        {
          required: false,
          type: 'string',
          message: '请输入粉丝数量',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      shopModalApp.shop = reactive({});
      if (routeId) {//路由
        shopModalApp.id = routeId;
        shopModalApp.display = false;
        findShop(shopModalApp.id);
      } else if (shopListApp.editModalForEdit) {//修改弹窗
        shopModalApp.id = shopListApp.currentId;
        shopModalApp.display = true;
      } else if (!shopListApp.editModalForEdit && shopListApp.addModalForEdit) {//新增弹窗
        shopModalApp.display = true;
      } else {
        shopModalApp.title = "新增学生";
      }
      if (shopModalApp.id) findShop(shopModalApp.id);
    })

    const findShop = (id) => {
      ShopService.findShopForView(id).then((res) => {
        shopModalApp.shop = res.data;
        //TODO:为编辑准备.
        shopModalApp.title = "编辑学生 " + shopModalApp.shop.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找学生出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        ShopService.updateShop(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        ShopService.saveShop(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/mall/merchant/shop`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        shopGrid.value.commitProxy("query");
      }, 1500);
      shopListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      shopListApp,
      shopModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
